package jgl.vajra.kingkong.part.home.pop;

import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentActivity;

import jgl.vajra.kingkong.R;
import jgl.vajra.kingkong.bean.MarketPriceBean;
import jgl.vajra.kingkong.databinding.PopOwnBidBinding;
import jgl.vajra.kingkong.widget.utils.ToastUtils;
import jgl.vajra.kingkong.widget.utils.UIUtils;

public class OwnBidPop {

    private FragmentActivity activity;
    private View mPopRootView;
    private PopupWindow menuPop;
    private OwnBidListener ownBidListener;
    private PopOwnBidBinding mBinding;
    private MarketPriceBean marketPriceBean;

    public OwnBidPop(FragmentActivity activity, MarketPriceBean marketPriceBean) {
        this.activity = activity;
        this.marketPriceBean = marketPriceBean;
    }


    private void initPopupWindow() {
        mPopRootView = View.inflate(UIUtils.getContext(), R.layout.pop_own_bid, null);
        mBinding = DataBindingUtil.bind(mPopRootView);
        mBinding.setData(marketPriceBean);

        menuPop = new PopupWindow(mPopRootView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        menuPop.setOutsideTouchable(false);
        menuPop.setBackgroundDrawable(new BitmapDrawable());
        menuPop.setFocusable(true);// 点击空白处时，隐藏掉pop窗口
        //输入框被键盘遮盖
        menuPop.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        menuPop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        menuPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                //popupwindow消失的时候恢复成原来的透明度
                backgroundAlpha(1f);
            }
        });
        mBinding.setView(this);


    }

    public void showPopupWindow(View v) {
        if (menuPop == null)
            initPopupWindow();
        int[] location = new int[2];
        v.getLocationOnScreen(location);  //获取控件的位置坐标
        //获取自身的长宽高
        mPopRootView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        backgroundAlpha(0.5f);

        menuPop.showAtLocation(v, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    public boolean isShowing() {
        return menuPop != null && menuPop.isShowing();
    }

    public void dismiss() {
        if (menuPop != null && menuPop.isShowing())
            menuPop.dismiss();
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        if (activity == null) return;
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        activity.getWindow().setAttributes(lp);

    }


    public void setOwnBidListener(OwnBidPop.OwnBidListener listener) {
        ownBidListener = listener;

    }

    public interface OwnBidListener {
        void onDetermine(String itemData);
    }

    //确定
    public void onDetermine() {
        String num = mBinding.etNum.getText().toString().trim();
        if (TextUtils.isEmpty(num)) {
            ToastUtils.showShort("我的出价不能为空");
            return;
        }

        if (Double.parseDouble(marketPriceBean.lowestPrice) > Double.parseDouble(num)) {
            ToastUtils.showShort("我的出价不能低于市场价格" + marketPriceBean.lowestPrice);
            return;
        }
        if (Double.parseDouble(marketPriceBean.highestPrice) < Double.parseDouble(num)) {
            ToastUtils.showShort("我的出价不能高于市场价格" + marketPriceBean.highestPrice);
            return;
        }

        dismiss();
        if (ownBidListener != null) {
            ownBidListener.onDetermine(num);
        }
    }
}

